<?php
	class CoaController extends Controller{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Accounting";
		public $subMenu = "";
		public $title = "";
		
		public function init() {
						
		}
		
		public function actionIndex(){
			$filter = Yii::app()->request->getParam("filter","");
			
			$data = array();
			$data["errorMessage"] = "";
			$data["formMessage"] = "";
			$arrCoa = array();
			
			
			$ssql = "select a.*,b.name as parent_name,b.acc_code as parent_acc_code from m_coa a left join m_coa b on a.parent_id = b.id where a.name like '%$filter%' order by a.acc_code";
			$conn = Yii::app()->db;
			$cmd = $conn->createCommand($ssql);
			$rs = $cmd->queryAll();
			
			
			
			$arrCoa = $rs;
			$data["arrCoa"] = $arrCoa;
			
			
			$this->layout ="//layouts/ly_blank";
			$this->render('index',$data);
		}
		
		public function actionAllList(){
			$ssql = "select * from m_coa";
			$conn = Yii::app()->db;
			$cmd = $conn->createCommand($ssql);
			$arrCustomerItem = $cmd->queryAll();
				
			$arrData = array();
			foreach($arrCustomerItem as $customerItem){
				$arrData[] = $customerItem;
			}
			$this->result["model"] = $arrData;
			$this->returnJSON($this->result);
		}
		
		public function actionEdit(){
			$filter = Yii::app()->request->getParam("filter","");
			$data = array();
			$data["errorMessage"] = "";
			$data["formMessage"] = "";
			
			$op = Yii::app()->request->getParam("op","");
			
			$id = Yii::app()->request->getParam("id","");
			$parentId = Yii::app()->request->getParam("parent_id","");
			$tmpAccCode = Yii::app()->request->getParam("tmp_acc_code","");
			$accName = Yii::app()->request->getParam("acc_name","");
			$notes = Yii::app()->request->getParam("notes","");
			
			$isLeaf = Yii::app()->request->getParam("is_leaf",1);
			$isLeaf = $isLeaf == "" ?1 : $isLeaf;
			
			$normalPosition = Yii::app()->request->getParam("normal_position","D");
			$normalPosition = $normalPosition == "" ? "D" : $normalPosition;
			
			$startBalance = Yii::app()->request->getParam("start_balance",0);
			$startBalance = $startBalance ==""?0:$startBalance;
			$startBalance = Utils::parseCurrency($startBalance);
			
			$balanceDate = Yii::app()->request->getParam("balance_date","01-Jan-2000");
			$balanceDate = $balanceDate ==""?"01-Jan-2000":$balanceDate;
			$balanceDate = Utils::formatDate($balanceDate);
			
			$isActive = Yii::app()->request->getParam("is_active",0);
			$isActive = $isActive === "on" ? 1 : 0;
			
			try{
				if ($op=="add"){
					//cek acc_code already exist;
					$mCoaParent = MCoa::model()->findByPK($parentId);
					$parent_acc_code = "";
					if ($mCoaParent){
						$parent_acc_code = $mCoaParent["acc_code"];
					} else{
						$data["formMessage"] .= "Invalid Parent Account <br>";						 
						throw new Exception($parentId . " not Found !");		
					}
					$acc_code = $parent_acc_code . "." .$tmpAccCode;
					$mCoa = MCoa::model()->find("acc_code = :accCode",array(":accCode"=>$acc_code));
					if ($mCoa){
						$data["formMessage"] .= $acc_code ." sudah terdaftar untuk " . $mCoa->name ."<br>";						
						throw new Exception($acc_code . " already exist !");
					}
					
					$mCoa = new MCoa();
					$mCoa->acc_code = $acc_code;
					$mCoa->name = $accName;
					$mCoa->notes = $notes;
					$mCoa->parent_id = $parentId;
					$mCoa->is_leaf = $isLeaf;
					if($isLeaf == 1){
						$mCoa->normal_position = $normalPosition;
						$mCoa->start_balance = $startBalance;
						$mCoa->balance_date = $balanceDate;
					} 
					$mCoa->is_active = 1;
					if(!$mCoa->save()){
						$data["formMessage"] .= "* Data Tidak dapat di simpan "."<br>";
						throw new Exception(json_encode($mCoa->getErrors()));
					}
					$data["formMessage"] .= "* Data telah di simpan "."<br>";
				}else if($op =="edit"){
					// cek is_active true or false (if change to not active and account not leaf, cek if all child account is not active)
					$mCoa = MCoa::model()->findByPK($id);
					if(!$mCoa){
						$data["formMessage"] .= "* ". Yii::t("","Data Tidak di temukan ") . "<br>";
						throw new Exception(json_encode($mCoa->getErrors()));
					}
					
					if($isActive == 0){
						$arrCek  =  MCoa::model()->find("acc_code like ':accCode' and is_active = 1",array(":accCode"=>$mCoa->acc_code."%"));
						if ($arrCek){
							$data["formMessage"] .= "* ". Yii::t("","Data Tidak di non aktifkan karena masih data child yang aktif.") . "<br>";
							throw new Exception("set not active error for ". $mCoa->id);
						}
					}
					
					
					$mCoaParent = MCoa::model()->findByPK($parentId);
					$parent_acc_code = "";
					if ($mCoaParent){
						$parent_acc_code = $mCoaParent["acc_code"];
					} else{
						$data["formMessage"] .= "Invalid Parent Account <br>";
						throw new Exception($parentId . " not Found !");
					}
					
					$acc_code = $parent_acc_code . "." .$tmpAccCode;
					if ($acc_code != $mCoa->acc_code){
						$accCek = MCoa::model()->find("acc_code = :accCode",array(":accCode"=>$acc_code));
						if ($accCek){
							$data["formMessage"] .= $acc_code ." sudah terdaftar untuk " . $mCoa->name ."<br>";
							throw new Exception($acc_code . " already exist !");
						}
					}
				
					$mCoa->acc_code = $acc_code;
					$mCoa->name = $accName;
					$mCoa->notes = $notes;
					if($mCoa->is_leaf == 1){
						$mCoa->normal_position = $normalPosition;
						$mCoa->start_balance = $startBalance;
						$mCoa->balance_date = $balanceDate;
					}
					$mCoa->is_active = $isActive;
					if(!$mCoa->save()){
						$data["formMessage"] .= "* Data Tidak dapat di simpan "."<br>";
						throw new Exception(json_encode($mCoa->getErrors()));
					}
					$data["formMessage"] .= "* Data telah di simpan "."<br>";
				}
			}catch (Exception $e){
				$data["errorMessage"] = $e->getMessage();				
			}
			
			$arrCoa = array();
			$ssql = "select a.*,b.name as parent_name,b.acc_code as parent_acc_code from m_coa a left join m_coa b on a.parent_id = b.id where a.name like '%$filter%' order by a.acc_code";
			$conn = Yii::app()->db;
			$cmd = $conn->createCommand($ssql);
			$rs = $cmd->queryAll();
			$arrCoa = $rs;
			$data["arrCoa"] = $arrCoa;
			$this->layout ="//layouts/ly_blank";
			$this->render('index',$data);
		}
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
	}
?>